package com.kx.game.center.servlet;

import com.alibaba.fastjson.JSONObject;
import com.kx.game.center.bean.UserBean;
import com.kx.game.center.common.PlayerEnum;
import com.kx.game.center.global.GSServices;
import com.kx.game.center.local.response.LocalErrorResponseCode;
import com.kx.game.center.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录记录
 */
@WebServlet(urlPatterns = "/report")
public class LoginReportServlet extends HttpServlet {

    private static Logger logger = LoggerFactory.getLogger(LoginReportServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            GSServices.loadManager.redisState();
            JSONObject requestDate = HttpUtil.getRequestDate(request);
            String create = (String) requestDate.get("create");//是否为创建
            String id = (String) requestDate.get("id");
            String wechatId = (String) requestDate.get("userName");
            String roleName = (String) requestDate.get("roleName");
            String iconUrl = (String) requestDate.get("iconUrl");
            String level = (String) requestDate.get("level");
            String serverId = (String) requestDate.get("serverId");
            String region = (String) requestDate.get("region");
            String sex = (String) requestDate.get("sex");
            String friendCode = (String) requestDate.get("friendCode");
            String roleId = (String) requestDate.get("roleId");
//            String constellation =(String) requestDate.get("constellation");//星座
            String userState = PlayerEnum.LOGIN.getIndex() + "";
            String lastUpdateTime = System.currentTimeMillis() + "";
            UserBean userInfo = GSServices.userManager.getUserInfo(wechatId);

            userInfo = GSServices.userManager.updateUserInfo(id, wechatId, roleName, iconUrl, level, userState,
                    lastUpdateTime, serverId, region, sex, friendCode, roleId, create, userInfo);

            if (userInfo != null && userInfo.getRegion() != region) { //地区变动
                GSServices.newFriendManager.saveRegionUser(region, wechatId, userInfo.getRegion());
            }
            if (friendCode != null && !friendCode.equals("") && !GSServices.userManager.existFriendCode(friendCode))
                GSServices.userManager.setFriendCode(friendCode, wechatId);
            //更新人数
            GSServices.localManager.incrementGSLoginCount(serverId, 1);
            GSServices.gameManager.printLog(request, requestDate, logger, "玩家登录");
            GSServices.localManager.sendMessage(response, true, null, "");
        } catch (Exception e) {
            e.printStackTrace();
            GSServices.localManager.sendMessage(response, false, LocalErrorResponseCode.SYSTEM_ERROR, "");
        }
    }
}
